﻿using LetsTestAPI.EDM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LetsTestAPI.Models;
using Newtonsoft.Json.Linq;

namespace LetsTestAPI.Controllers
{
    public class MOKController : ApiController
    {
        public string GetCandidates(bool IsRecruitmentOpen)
        {
            using (LetsTestEntities Context = new LetsTestEntities())
            {
                List<CandidateModel> candModLis = new List<CandidateModel>();

                var list = Context.Candidates.Where(x => x.IsActive == true && x.IsRecruitmentOpened == IsRecruitmentOpen);

                if (list != null && list.Count() > 0)
                {
                    foreach (var item in list)
                    {
                        candModLis.Add(new CandidateModel(item));
                    }
                }

                JArray ja = JArray.FromObject(candModLis);
                return ja.ToString();
            }
        }

        public string GetSpecialists()
        {
            using (LetsTestEntities Context = new LetsTestEntities())
            {
                List<SpecialistModel> specModLis = new List<SpecialistModel>();

                var list = Context.Specialists.Where(x => x.IsActive == true);

                if (list != null && list.Count() > 0)
                {
                    foreach (var item in list)
                    {
                        specModLis.Add(new SpecialistModel(item));
                    }
                }

                JArray ja = JArray.FromObject(specModLis);
                return ja.ToString();
            }
        }

        public string GetCandidate(int CandidateID)
        {
            using (LetsTestEntities Context = new LetsTestEntities())
            {
                Candidate selectedCandidate = null;

                var candidates = Context.Candidates.Where(x => x.Id == CandidateID && x.IsActive == true);

                if (candidates != null && candidates.Count() > 0)
                {
                    selectedCandidate = candidates.FirstOrDefault();

                    JObject jo = JObject.FromObject(new CandidateModel(selectedCandidate));

                    return jo.ToString();
                }


                return null;
            }
        }

        public string GetSpecialist(int SpecialistID)
        {
            using (LetsTestEntities Context = new LetsTestEntities())
            {
                Specialist selectedSpecialist = null;

                var specialists = Context.Specialists.Where(x => x.Id == SpecialistID && x.IsActive == true);

                if (specialists != null && specialists.Count() > 0)
                {
                    selectedSpecialist = specialists.FirstOrDefault();

                    JObject jo = JObject.FromObject(new SpecialistModel(selectedSpecialist));

                    return jo.ToString();
                }

                return null;
            }
        }

        public string GetSpecialistsInTechnology(int TechnologyID)
        {
            using (LetsTestEntities Context = new LetsTestEntities())
            {
                List<SpecialistModel> list = new List<SpecialistModel>();

                var specialistsInTechnologies = Context.SpecialistsInTechnologies.Where(x => x.TechnologyID == TechnologyID && x.IsActive == true);

                if (specialistsInTechnologies != null && specialistsInTechnologies.Count() > 0)
                {
                    foreach (var item in specialistsInTechnologies)
                    {
                        list.Add(new SpecialistModel(Context.Specialists.Where(x => x.UserID == item.User.Id).FirstOrDefault()));
                    }

                    JArray ja = JArray.FromObject(list);
                    return ja.ToString();
                }

                return null;
            }
        }
    }
}
